{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据挖掘 — 今日头条微信公众号内容抓取"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 今日头条微信公众号内容"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 项目要求\n",
    "* 使用selenium进入微信公众平台\n",
    "* 在微信公众平台寻找指定的公众号\n",
    "* 抓取该公众号指定时间区间的文章（不低于50页数据/不低于1年的数据）\n",
    "* 导出文章信息（应包含标题，时间，文章url链接以及文章文本内容）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 准备工作，导入模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from lxml.html import fromstring\n",
    "import time\n",
    "from random import random\n",
    "from requests_html import HTMLSession"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-2-249baf105334>:9: DeprecationWarning: use options instead of chrome_options\n",
      "  driver = webdriver.Chrome( chrome_options = opts) #desired_capabilities=caps,a\n"
     ]
    }
   ],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.desired_capabilities import DesiredCapabilities\n",
    "opts = webdriver.ChromeOptions()\n",
    "opts.add_argument('--no-sandbox')#解决DevToolsActivePort文件不存在的报错\n",
    "opts.add_argument('window-size=1920x3000') #指定浏览器分辨率\n",
    "opts.add_argument('--disable-gpu') #谷歌文档提到需要加上一这个属性来规避bug\n",
    "opts.add_argument('--hide-scrollbars') #隐藏滚动条, 应对些特殊页面\n",
    "\n",
    "driver = webdriver.Chrome( chrome_options = opts) #desired_capabilities=caps,a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 输入“公众号”参数\n",
    "公众号 = \"今日头条\"\n",
    "# 指定内容输出的位置\n",
    "fn = { \"output\" : { \"公众号_htm_snippets\": \"data_raw_src_/公众号_htm_snippets_{公众号}.tsv\",\n",
    "                    \"公众号_df\": \"data_raw_src_/公众号_df_{公众号}.tsv\",\n",
    "                    \"公众号_xlsx\": \"公众号_url_{公众号}.xlsx\" } \\\n",
    "      }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 登陆的网址\n",
    "driver.get(\"https://mp.weixin.qq.com\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 登陆微信公众号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "payload =  {\"account\": \"fengyf190028@stu.nfu.edu.cn\", \"password\": \"f8312266.\"}\n",
    "# payload =  {\"account\": \"NFUHacks@163.com\", \"password\": \"NFU706947580\"}\n",
    "# 切换为账号密码登录\n",
    "driver.find_element_by_xpath('//div[@class=\"login__type__container login__type__container__scan\"]/a').click()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 输入账号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 清空账号input\n",
    "driver.find_element_by_xpath('//form[@class=\"login_form\"]//input[@name=\"account\"]').clear()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver.find_element_by_xpath('//form[@class=\"login_form\"]//input[@name=\"account\"]').send_keys(payload['account'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 输入登陆密码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 清空密码input\n",
    "driver.find_element_by_xpath('//form[@class=\"login_form\"]//input[@name=\"password\"]').clear()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver.find_element_by_xpath('//form[@class=\"login_form\"]//input[@name=\"password\"]').send_keys(payload['password'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 点击登陆按钮"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver.find_element_by_xpath('//div[@class=\"login_btn_panel\"]/a').click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 点击+\n",
    "element = driver.find_element_by_xpath('//div[@class=\"weui-desktop-panel\"]//div[@class=\"new-creation__menu-item\"]')\n",
    "element.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['CDwindow-E6295641BF05C9BB1C080C8FBBA5E5A0', 'CDwindow-0715C90757F36F9D79E6C230BE3AEEDB']\n"
     ]
    }
   ],
   "source": [
    "# 检查窗口信息\n",
    "print (driver.window_handles)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-14-6c6d5ce6602d>:1: DeprecationWarning: use driver.switch_to.window instead\n",
      "  driver.switch_to_window(driver.window_handles[1])\n"
     ]
    }
   ],
   "source": [
    "driver.switch_to_window(driver.window_handles[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 点击超链接\n",
    "element = driver.find_element_by_xpath('//div[@class=\"weui-desktop-head__inner js_header_inner\"]//div[@class=\"media_list_box_inner\"]//ul[@class=\"tpl_list\"]/li[@class=\"tpl_item\"][1]')\n",
    "element.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 点击选择其他公众号\n",
    "element = driver.find_element_by_xpath('//div[@class=\"weui-desktop-dialog__bd\"]//div[@class=\"weui-desktop-form__control-group\"]//div[@class=\"weui-desktop-form__controls\"]//div[@class=\"weui-desktop-btn_wrp weui-desktop-link-btn weui-desktop-link\"]')\n",
    "element.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 点击今日头条\n",
    "driver.find_element_by_xpath('//*[@id=\"vue_app\"]/div[2]/div[1]/div/div[2]/div[2]/form[1]/div[3]/div/div/div/div/span/input').clear()\n",
    "driver.find_element_by_xpath('//*[@id=\"vue_app\"]/div[2]/div[1]/div/div[2]/div[2]/form[1]/div[3]/div/div/div/div/span/input').send_keys(\"今日头条\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<div class=\"weui-desktop-icon weui-desktop-icon__search weui-desktop-icon__small\" style=\"width: 20px; height: 20px;\"><!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!---->     <svg viewBox=\"0 0 24 24\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><title>MP/Icon/Search</title> <g id=\"MP/Icon/Search\" stroke=\"none\" stroke-width=\"1\" fill=\"none\" fill-rule=\"evenodd\"><path d=\"M5.78025253,5.78248558 C8.51392257,3.04881554 12.9460774,3.04881554 15.6797475,5.78248558 C18.1730922,8.27583028 18.3922898,12.1821488 16.3373403,14.9239313 L20.6294949,19.2175144 L19.2152814,20.631728 L14.922508,16.3389663 C12.180685,18.394566 8.27384272,18.1755707 5.78025253,15.6819805 C3.04658249,12.9483105 3.04658249,8.51615562 5.78025253,5.78248558 Z M6.8409127,6.84314575 C4.6930291,8.99102935 4.6930291,12.4734367 6.8409127,14.6213203 C8.98879631,16.7692039 12.4712037,16.7692039 14.6190873,14.6213203 C16.7669709,12.4734367 16.7669709,8.99102935 14.6190873,6.84314575 C12.4712037,4.69526215 8.98879631,4.69526215 6.8409127,6.84314575 Z\" id=\"形状\"></path></g></svg> <!----> <!----> <!----> <!----> <!----></div>\n"
     ]
    }
   ],
   "source": [
    "# 点放大镜搜\n",
    "element = driver.find_element_by_xpath('//button[@class=\"weui-desktop-icon-btn weui-desktop-search__btn\"]')\n",
    "main_content = element.get_attribute('innerHTML')\n",
    "print(main_content)\n",
    "element.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<li class=\"inner_link_account_item\"><div class=\"weui-desktop-vm_primary\"><img src=\"http://mmbiz.qpic.cn/mmbiz_png/GW0wZ5tpb8axdZLrcR3sGdpVaJMSbypvLsEwlq8FLVMUBmpzZRz7OY0yjDRjnRLcLqoic0aNV1EW5762sH3xcyw/0?wx_fmt=png\" class=\"inner_link_account_avatar\"> <strong class=\"inner_link_account_nickname\">今日头条</strong> <i class=\"inner_link_account_wechat\">微信号：headline_today</i></div> <div class=\"weui-desktop-vm_default inner_link_account_type\">订阅号</div></li><li class=\"inner_link_account_item\"><div class=\"weui-desktop-vm_primary\"><img src=\"http://mmbiz.qpic.cn/sz_mmbiz_png/V5ZhzcVoZDqUbjtN9M7bsD85ZZGPrd5X8IOsdaJrIiaZbqaT16JcF2F9ttSVHXa1w2LMnGC6LujzDY2ZV3tIomA/0?wx_fmt=png\" class=\"inner_link_account_avatar\"> <strong class=\"inner_link_account_nickname\">今日头条服务号</strong> <i class=\"inner_link_account_wechat\">微信号：未设置</i></div> <div class=\"weui-desktop-vm_default inner_link_account_type\">服务号</div></li><li class=\"inner_link_account_item\"><div class=\"weui-desktop-vm_primary\"><img src=\"http://mmbiz.qpic.cn/mmbiz_png/fKw1sskyd9g0Kib3qoWTL0D9HF4XSAmvKZEGuCjYPiaSd2lllgJy1T9YmPfTrSExBDGiaseialbDKxT6lOXibIGlr0g/0?wx_fmt=png\" class=\"inner_link_account_avatar\"> <strong class=\"inner_link_account_nickname\">今日头条有限公司</strong> <i class=\"inner_link_account_wechat\">微信号：未设置</i></div> <div class=\"weui-desktop-vm_default inner_link_account_type\">服务号</div></li><li class=\"inner_link_account_item\"><div class=\"weui-desktop-vm_primary\"><img src=\"http://mmbiz.qpic.cn/mmbiz_png/5jOFcm5ph7EyjQqzdBibBk8BhfFs9UOfmHkYibhsnib99vicnVAB89H4ndvjQTLia8J6NM9NgTl2R3ic9zFLmOkhQnnQ/0?wx_fmt=png\" class=\"inner_link_account_avatar\"> <strong class=\"inner_link_account_nickname\">今日视频头条</strong> <i class=\"inner_link_account_wechat\">微信号：dailyvideoheadline</i></div> <div class=\"weui-desktop-vm_default inner_link_account_type\">订阅号</div></li><li class=\"inner_link_account_item\"><div class=\"weui-desktop-vm_primary\"><img src=\"http://mmbiz.qpic.cn/mmbiz_png/sNvv9XBSlMna4SUAUzE8byfVqSPSpZ6r3sV2LV7nRefcpPrGa4R53niaAJVx0TJb7JRvAjKjBtmrvbu3oNN2Uow/0?wx_fmt=png\" class=\"inner_link_account_avatar\"> <strong class=\"inner_link_account_nickname\">今日头条UED</strong> <i class=\"inner_link_account_wechat\">微信号：ByteDance_GIP</i></div> <div class=\"weui-desktop-vm_default inner_link_account_type\">订阅号</div></li>\n"
     ]
    }
   ],
   "source": [
    "element = driver.find_element_by_xpath('//ul[@class=\"inner_link_account_list\"]')\n",
    "main_content = element.get_attribute('innerHTML')\n",
    "print(main_content)\n",
    "公众号SERP = main_content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 解析\n",
    "import pandas as pd\n",
    "from lxml.html import fromstring\n",
    "root = fromstring(公众号SERP) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>nickname</th>\n",
       "      <th>wechat</th>\n",
       "      <th>img</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>今日头条</td>\n",
       "      <td>微信号：headline_today</td>\n",
       "      <td>http://mmbiz.qpic.cn/mmbiz_png/GW0wZ5tpb8axdZL...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>今日头条服务号</td>\n",
       "      <td>微信号：未设置</td>\n",
       "      <td>http://mmbiz.qpic.cn/sz_mmbiz_png/V5ZhzcVoZDqU...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>今日头条有限公司</td>\n",
       "      <td>微信号：未设置</td>\n",
       "      <td>http://mmbiz.qpic.cn/mmbiz_png/fKw1sskyd9g0Kib...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>今日视频头条</td>\n",
       "      <td>微信号：dailyvideoheadline</td>\n",
       "      <td>http://mmbiz.qpic.cn/mmbiz_png/5jOFcm5ph7EyjQq...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>今日头条UED</td>\n",
       "      <td>微信号：ByteDance_GIP</td>\n",
       "      <td>http://mmbiz.qpic.cn/mmbiz_png/sNvv9XBSlMna4SU...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   nickname                  wechat  \\\n",
       "0      今日头条      微信号：headline_today   \n",
       "1   今日头条服务号                 微信号：未设置   \n",
       "2  今日头条有限公司                 微信号：未设置   \n",
       "3    今日视频头条  微信号：dailyvideoheadline   \n",
       "4   今日头条UED       微信号：ByteDance_GIP   \n",
       "\n",
       "                                                 img  \n",
       "0  http://mmbiz.qpic.cn/mmbiz_png/GW0wZ5tpb8axdZL...  \n",
       "1  http://mmbiz.qpic.cn/sz_mmbiz_png/V5ZhzcVoZDqU...  \n",
       "2  http://mmbiz.qpic.cn/mmbiz_png/fKw1sskyd9g0Kib...  \n",
       "3  http://mmbiz.qpic.cn/mmbiz_png/5jOFcm5ph7EyjQq...  \n",
       "4  http://mmbiz.qpic.cn/mmbiz_png/sNvv9XBSlMna4SU...  "
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "主 = root.xpath('//li[@class=\"inner_link_account_item\"]')\n",
    "\n",
    "account_list = []\n",
    "for e in 主:\n",
    "    account_nickname = e.xpath('./div/strong[@class=\"inner_link_account_nickname\"]')[0].text\n",
    "    account_wechat = e.xpath('./div/i[@class=\"inner_link_account_wechat\"]')[0].text\n",
    "    account_img = e.xpath('./div/img/@src')[0]\n",
    "    account = {\"nickname\": account_nickname, \"wechat\": account_wechat, \"img\": account_img,}\n",
    "    account_list.append(account)\n",
    "\n",
    "df_account = pd.DataFrame(account_list)\n",
    "df_account"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<div class=\"weui-desktop-vm_primary\"><img src=\"http://mmbiz.qpic.cn/mmbiz_png/GW0wZ5tpb8axdZLrcR3sGdpVaJMSbypvLsEwlq8FLVMUBmpzZRz7OY0yjDRjnRLcLqoic0aNV1EW5762sH3xcyw/0?wx_fmt=png\" class=\"inner_link_account_avatar\"> <strong class=\"inner_link_account_nickname\">今日头条</strong> <i class=\"inner_link_account_wechat\">微信号：headline_today</i></div> <div class=\"weui-desktop-vm_default inner_link_account_type\">订阅号</div>\n"
     ]
    }
   ],
   "source": [
    "element = driver.find_element_by_xpath('//ul[@class=\"inner_link_account_list\"]/li')\n",
    "main_content = element.get_attribute('innerHTML')\n",
    "print(main_content)\n",
    "element.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n跳转_input = driver.find_element_by_xpath(\\'//span[@class=\"weui-desktop-pagination__form\"]/input\\')\\n跳转_a = driver.find_element_by_xpath(\\'//span[@class=\"weui-desktop-pagination__form\"]/a\\')\\n跳转_title = driver.find_element_by_xpaht(\\'//div[@class=\"inner_link_article_title\"]//span//text()\\')\\n跳转_input.clear()\\n跳转_input.send_keys(2)\\n跳转_a.click()\\n'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 跳转testing\n",
    "'''\n",
    "跳转_input = driver.find_element_by_xpath('//span[@class=\"weui-desktop-pagination__form\"]/input')\n",
    "跳转_a = driver.find_element_by_xpath('//span[@class=\"weui-desktop-pagination__form\"]/a')\n",
    "跳转_title = driver.find_element_by_xpaht('//div[@class=\"inner_link_article_title\"]//span//text()')\n",
    "跳转_input.clear()\n",
    "跳转_input.send_keys(2)\n",
    "跳转_a.click()\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 314]\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# 跳转上限\n",
    "l_e = driver.find_elements_by_xpath('//label[@class=\"weui-desktop-pagination__num\"]')\n",
    "l_e_int  = [int(x.text) for x in l_e] \n",
    "print (l_e_int)\n",
    "print (l_e_int[0]==l_e_int[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314]\n"
     ]
    }
   ],
   "source": [
    "pages = list(range(l_e_int[0],l_e_int[-1]+1 ))\n",
    "#print(pages[0:2])\n",
    "pages = list(range(1,l_e_int[-1]+1 ))\n",
    "print(pages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 获取前 60 页的内容\n",
    "pages = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "# global varialbes \n",
    "# 循环 遍历\n",
    "html_raw = dict()\n",
    "main_content =\"\"\n",
    "element = None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "def process_pages (pages):\n",
    "    for p in pages:\n",
    "        print (p,end='\\t')\n",
    "\n",
    "        跳转_input = driver.find_element_by_xpath('//span[@class=\"weui-desktop-pagination__form\"]/input')\n",
    "        跳转_a = driver.find_element_by_xpath('//span[@class=\"weui-desktop-pagination__form\"]/a')\n",
    "        跳转_input.clear()\n",
    "        跳转_input.send_keys(p)\n",
    "        跳转_a.click()\n",
    "\n",
    "        time.sleep(45+120*random())\n",
    "\n",
    "        element = driver.find_element_by_xpath('//div[@class=\"inner_link_article_list\"]')\n",
    "        main_content = element.get_attribute('innerHTML')\n",
    "        #print(main_content)\n",
    "        html_raw[p] = main_content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\t2\t3\t4\t5\t6\t7\t8\t9\t10\t11\t12\t13\t14\t15\t16\t17\t18\t19\t20\t21\t22\t23\t24\t25\t26\t27\t28\t29\t30\t31\t32\t33\t34\t35\t36\t37\t38\t39\t40\t41\t42\t43\t44\t45\t46\t47\t48\t49\t50\t51\t52\t53\t54\t55\t56\t57\t58\t59\t60\t"
     ]
    }
   ],
   "source": [
    "process_pages (pages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>html_snippets</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>&lt;div class=\"weui-desktop-radio-group\"&gt;&lt;label c...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                       html_snippets\n",
       "1  <div class=\"weui-desktop-radio-group\"><label c..."
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame([html_raw]).T\n",
    "df.columns = [\"html_snippets\"]\n",
    "df.loc[0:1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Stored 'html_raw' (dict)\n"
     ]
    }
   ],
   "source": [
    "%store html_raw\n",
    "import pickle \n",
    "filehandler = open(\"html_raw\", 'wb') \n",
    "pickle.dump(html_raw, filehandler)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "59\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>html_snippets</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>&lt;div class=\"weui-desktop-radio-group\"&gt;&lt;label c...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                        html_snippets\n",
       "12  <div class=\"weui-desktop-radio-group\"><label c..."
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# df.duplicated()  默认所有列，无重复记录  【duplicated()函数】判断是否有重复项\n",
    "df_out = df[~df.duplicated()]\n",
    "print (len(df_out))\n",
    "df[df.duplicated()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[12]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[12]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "try_again = list(df[df.duplicated()].index)\n",
    "print(try_again)\n",
    "try_again = try_again + list (set(pages).difference(set(df.index.values)))\n",
    "try_again"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 暂存档\n",
    "filename = fn [\"output\"] [\"公众号_htm_snippets\"] \n",
    "df_out.to_csv(filename.format(公众号=公众号), sep=\"\\t\", encoding=\"utf8\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5,5,5,5,5,6,6,6,5,5,6,5,5,5,5,5,5,5,5,5,5,7,5,5,6,7,5,6,5,5,7,5,5,6,5,5,5,6,8,12,5,7,5,5,6,5,5,5,5,6,5,5,5,5,5,5,5,5,5,6,"
     ]
    }
   ],
   "source": [
    "def get_content(link):\n",
    "    session = HTMLSession()\n",
    "    r = session.get(url=link)\n",
    "    content_xpath_1 = '//*[@id=\"js_content\"]//span/text()'\n",
    "    content_xpath_2 = '//*[@id=\"js_content\"]//p/text()'\n",
    "    content_1 = ''.join(r.html.xpath(content_xpath_1))\n",
    "    content_2 = ''.join(r.html.xpath(content_xpath_2))\n",
    "    return content_1 + content_2\n",
    "\n",
    "def parse_html_snippets(_snippet_):\n",
    "    root = fromstring(_snippet_) \n",
    "    title = [x.text for x in root.xpath('//div[@class=\"inner_link_article_title\"]//span[2]')]\n",
    "    create_time = [x.text for x in root.xpath('//div[@class=\"inner_link_article_date\"]')]\n",
    "    link = [x for x in root.xpath('//a/@href')]\n",
    "    content_text = [get_content(x) for x in link]\n",
    "    _df_ = pd.DataFrame({\"title\":title, \"create_time\": create_time, \"link\":link, \"content_text\":content_text})\n",
    "    return(_df_)\n",
    "    \n",
    "l_df = []\n",
    "for p in pages:\n",
    "    _df_ = parse_html_snippets(df.loc[p,\"html_snippets\"])\n",
    "    print (len(_df_), end=\",\")\n",
    "    l_df.append(_df_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>title</th>\n",
       "      <th>create_time</th>\n",
       "      <th>link</th>\n",
       "      <th>content_text</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>村主任帮人寻亲，3年“捡”回100多人</td>\n",
       "      <td>2021-05-26</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...</td>\n",
       "      <td>“多年坚守，在抖音帮人寻亲助百人回家；三进火场，全身烧伤后用歌声回报社会；六年陪伴，用绘画为...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>走啊，一起穿越？</td>\n",
       "      <td>2021-05-18</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...</td>\n",
       "      <td>荆州博物馆  楚·玉覆面  鄂尔多斯青铜器博物馆  包金卧羊带具  四川广汉三星堆博物馆...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>被拐80年后，93岁的老奶奶终于回到了家</td>\n",
       "      <td>2021-05-17</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...</td>\n",
       "      <td>一条回家的路可以走多远？2020年疫情期间，寻人志愿者刘红涛在抖音上收到了一条粉丝留言，说是...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>刚刚，天问一号登陆火星！</td>\n",
       "      <td>2021-05-15</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...</td>\n",
       "      <td>就在今晨，天问一号登陆火星了！天问一号是我国火星探测计划中的一个重要部分。2020年7月23...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>重回大山的“乞丐校长”和他的幼儿园</td>\n",
       "      <td>2021-05-14</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...</td>\n",
       "      <td>“一块荒地上，”王龙羽每天最幸福的时刻，就是看着幼儿园里的孩子们入睡。山村里的星星很亮，结束...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>324</th>\n",
       "      <td>今日头条“灵犬”背后的反低俗技术原理</td>\n",
       "      <td>2019-07-31</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...</td>\n",
       "      <td>2018年3月至今，今日头条推出的灵犬反低俗助手已经经过了三次迭代。昨天，“灵犬3.0”正式...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>325</th>\n",
       "      <td>今日头条新版“灵犬”来了，支持图片反低俗</td>\n",
       "      <td>2019-07-30</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...</td>\n",
       "      <td>▲一分钟了解灵犬反低俗助手大家好，在这个炎热的夏天，灵犬反低俗助手迎来了重要升级：新版“灵犬...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>326</th>\n",
       "      <td>每天有845万人等着他们更新！这些头条上的外国人火了！</td>\n",
       "      <td>2019-07-29</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...</td>\n",
       "      <td>外国人会唱的第一首中文歌是哪个？剥蛋壳对美国人来说有多难？拿着中国驾照可以在德国开车吗？今日...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>327</th>\n",
       "      <td>被百万人点赞的3个真实故事，网友：从头哭到尾</td>\n",
       "      <td>2019-07-25</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...</td>\n",
       "      <td>第1个是关于赴台老兵的故事。视频中的主人公名叫刘德文，是台湾高雄左营区祥和里的里长，也是头条...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>328</th>\n",
       "      <td>帮10000个人回家，要多久？</td>\n",
       "      <td>2019-07-25</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...</td>\n",
       "      <td>从团结湖地铁站东南口出来，步行到家的时间是15分钟。三年多来，头条寻人项目负责人曾华已经习惯...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>329 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                           title create_time  \\\n",
       "0            村主任帮人寻亲，3年“捡”回100多人  2021-05-26   \n",
       "1                       走啊，一起穿越？  2021-05-18   \n",
       "2           被拐80年后，93岁的老奶奶终于回到了家  2021-05-17   \n",
       "3                   刚刚，天问一号登陆火星！  2021-05-15   \n",
       "4              重回大山的“乞丐校长”和他的幼儿园  2021-05-14   \n",
       "..                           ...         ...   \n",
       "324           今日头条“灵犬”背后的反低俗技术原理  2019-07-31   \n",
       "325         今日头条新版“灵犬”来了，支持图片反低俗  2019-07-30   \n",
       "326  每天有845万人等着他们更新！这些头条上的外国人火了！  2019-07-29   \n",
       "327       被百万人点赞的3个真实故事，网友：从头哭到尾  2019-07-25   \n",
       "328              帮10000个人回家，要多久？  2019-07-25   \n",
       "\n",
       "                                                  link  \\\n",
       "0    http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...   \n",
       "1    http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...   \n",
       "2    http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...   \n",
       "3    http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...   \n",
       "4    http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...   \n",
       "..                                                 ...   \n",
       "324  http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...   \n",
       "325  http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...   \n",
       "326  http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...   \n",
       "327  http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...   \n",
       "328  http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...   \n",
       "\n",
       "                                          content_text  \n",
       "0    “多年坚守，在抖音帮人寻亲助百人回家；三进火场，全身烧伤后用歌声回报社会；六年陪伴，用绘画为...  \n",
       "1      荆州博物馆  楚·玉覆面  鄂尔多斯青铜器博物馆  包金卧羊带具  四川广汉三星堆博物馆...  \n",
       "2    一条回家的路可以走多远？2020年疫情期间，寻人志愿者刘红涛在抖音上收到了一条粉丝留言，说是...  \n",
       "3    就在今晨，天问一号登陆火星了！天问一号是我国火星探测计划中的一个重要部分。2020年7月23...  \n",
       "4    “一块荒地上，”王龙羽每天最幸福的时刻，就是看着幼儿园里的孩子们入睡。山村里的星星很亮，结束...  \n",
       "..                                                 ...  \n",
       "324  2018年3月至今，今日头条推出的灵犬反低俗助手已经经过了三次迭代。昨天，“灵犬3.0”正式...  \n",
       "325  ▲一分钟了解灵犬反低俗助手大家好，在这个炎热的夏天，灵犬反低俗助手迎来了重要升级：新版“灵犬...  \n",
       "326  外国人会唱的第一首中文歌是哪个？剥蛋壳对美国人来说有多难？拿着中国驾照可以在德国开车吗？今日...  \n",
       "327  第1个是关于赴台老兵的故事。视频中的主人公名叫刘德文，是台湾高雄左营区祥和里的里长，也是头条...  \n",
       "328  从团结湖地铁站东南口出来，步行到家的时间是15分钟。三年多来，头条寻人项目负责人曾华已经习惯...  \n",
       "\n",
       "[329 rows x 4 columns]"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_url_out = pd.concat(l_df).reset_index(drop=True)\n",
    "df_url_out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA==&mid=2661922277&idx=1&sn=ae7d4aedbc17e8a202d6d781b00868af&chksm=bd9269f68ae5e0e079cd98c054faf98999dd17721cd8be2f112f44c6d4cb0acbec479dda7c45#rd'"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 试验公众号文章链接是否正确\n",
    "df_url_out.loc[0].link"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>title</th>\n",
       "      <th>create_time</th>\n",
       "      <th>link</th>\n",
       "      <th>content_text</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>59</th>\n",
       "      <td>一张5毛钱，让他“惦记”了20多年</td>\n",
       "      <td>2020-11-17</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...</td>\n",
       "      <td>小时候，小蔡的父母经常不在家，所以从小他就学着母亲的样子，给自己和妹妹做饭：洗米下锅、用树枝...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>60</th>\n",
       "      <td>关于蒙古送的三万只羊，你可能还想知道这些</td>\n",
       "      <td>2020-11-16</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...</td>\n",
       "      <td>15日下午6点，武汉市文化与旅游局发布消息，首批1.2万经过加工的蒙古国捐赠羊，已经抵达武汉...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>61</th>\n",
       "      <td>裸辞工作回家做饭，这个山东男人用美食还原了80、90后的童年</td>\n",
       "      <td>2020-11-13</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...</td>\n",
       "      <td>对于二次元动漫迷来说，cosplay是一件乐趣无穷的事。那么，你见过cos二次元美食的吗？在...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>62</th>\n",
       "      <td>从北京返乡的90后村官，和他的猪，一起红了</td>\n",
       "      <td>2020-11-10</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...</td>\n",
       "      <td>你是什么时候开始用今日头条的？今日头条为你的生活和工作带来过哪些改变？前不久，我们发起了#我...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>63</th>\n",
       "      <td>40岁大叔花4400小时仿造杭州，3个月复原红楼梦，引70万人惊叹</td>\n",
       "      <td>2020-11-06</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...</td>\n",
       "      <td>街东宁国府，街西荣国府，再到大观园，占地千亩，绿树红花，小桥流水，一应俱全。以上语句形容的是...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                title create_time  \\\n",
       "59                  一张5毛钱，让他“惦记”了20多年  2020-11-17   \n",
       "60               关于蒙古送的三万只羊，你可能还想知道这些  2020-11-16   \n",
       "61     裸辞工作回家做饭，这个山东男人用美食还原了80、90后的童年  2020-11-13   \n",
       "62              从北京返乡的90后村官，和他的猪，一起红了  2020-11-10   \n",
       "63  40岁大叔花4400小时仿造杭州，3个月复原红楼梦，引70万人惊叹  2020-11-06   \n",
       "\n",
       "                                                 link  \\\n",
       "59  http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...   \n",
       "60  http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...   \n",
       "61  http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...   \n",
       "62  http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...   \n",
       "63  http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...   \n",
       "\n",
       "                                         content_text  \n",
       "59  小时候，小蔡的父母经常不在家，所以从小他就学着母亲的样子，给自己和妹妹做饭：洗米下锅、用树枝...  \n",
       "60  15日下午6点，武汉市文化与旅游局发布消息，首批1.2万经过加工的蒙古国捐赠羊，已经抵达武汉...  \n",
       "61  对于二次元动漫迷来说，cosplay是一件乐趣无穷的事。那么，你见过cos二次元美食的吗？在...  \n",
       "62  你是什么时候开始用今日头条的？今日头条为你的生活和工作带来过哪些改变？前不久，我们发起了#我...  \n",
       "63  街东宁国府，街西荣国府，再到大观园，占地千亩，绿树红花，小桥流水，一应俱全。以上语句形容的是...  "
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 找出重复项\n",
    "df_url_out[df_url_out.duplicated()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>title</th>\n",
       "      <th>create_time</th>\n",
       "      <th>link</th>\n",
       "      <th>content_text</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>村主任帮人寻亲，3年“捡”回100多人</td>\n",
       "      <td>2021-05-26</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...</td>\n",
       "      <td>“多年坚守，在抖音帮人寻亲助百人回家；三进火场，全身烧伤后用歌声回报社会；六年陪伴，用绘画为...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>走啊，一起穿越？</td>\n",
       "      <td>2021-05-18</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...</td>\n",
       "      <td>荆州博物馆  楚·玉覆面  鄂尔多斯青铜器博物馆  包金卧羊带具  四川广汉三星堆博物馆...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>被拐80年后，93岁的老奶奶终于回到了家</td>\n",
       "      <td>2021-05-17</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...</td>\n",
       "      <td>一条回家的路可以走多远？2020年疫情期间，寻人志愿者刘红涛在抖音上收到了一条粉丝留言，说是...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>刚刚，天问一号登陆火星！</td>\n",
       "      <td>2021-05-15</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...</td>\n",
       "      <td>就在今晨，天问一号登陆火星了！天问一号是我国火星探测计划中的一个重要部分。2020年7月23...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>重回大山的“乞丐校长”和他的幼儿园</td>\n",
       "      <td>2021-05-14</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...</td>\n",
       "      <td>“一块荒地上，”王龙羽每天最幸福的时刻，就是看着幼儿园里的孩子们入睡。山村里的星星很亮，结束...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>324</th>\n",
       "      <td>今日头条“灵犬”背后的反低俗技术原理</td>\n",
       "      <td>2019-07-31</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...</td>\n",
       "      <td>2018年3月至今，今日头条推出的灵犬反低俗助手已经经过了三次迭代。昨天，“灵犬3.0”正式...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>325</th>\n",
       "      <td>今日头条新版“灵犬”来了，支持图片反低俗</td>\n",
       "      <td>2019-07-30</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...</td>\n",
       "      <td>▲一分钟了解灵犬反低俗助手大家好，在这个炎热的夏天，灵犬反低俗助手迎来了重要升级：新版“灵犬...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>326</th>\n",
       "      <td>每天有845万人等着他们更新！这些头条上的外国人火了！</td>\n",
       "      <td>2019-07-29</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...</td>\n",
       "      <td>外国人会唱的第一首中文歌是哪个？剥蛋壳对美国人来说有多难？拿着中国驾照可以在德国开车吗？今日...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>327</th>\n",
       "      <td>被百万人点赞的3个真实故事，网友：从头哭到尾</td>\n",
       "      <td>2019-07-25</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...</td>\n",
       "      <td>第1个是关于赴台老兵的故事。视频中的主人公名叫刘德文，是台湾高雄左营区祥和里的里长，也是头条...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>328</th>\n",
       "      <td>帮10000个人回家，要多久？</td>\n",
       "      <td>2019-07-25</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...</td>\n",
       "      <td>从团结湖地铁站东南口出来，步行到家的时间是15分钟。三年多来，头条寻人项目负责人曾华已经习惯...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>324 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                           title create_time  \\\n",
       "0            村主任帮人寻亲，3年“捡”回100多人  2021-05-26   \n",
       "1                       走啊，一起穿越？  2021-05-18   \n",
       "2           被拐80年后，93岁的老奶奶终于回到了家  2021-05-17   \n",
       "3                   刚刚，天问一号登陆火星！  2021-05-15   \n",
       "4              重回大山的“乞丐校长”和他的幼儿园  2021-05-14   \n",
       "..                           ...         ...   \n",
       "324           今日头条“灵犬”背后的反低俗技术原理  2019-07-31   \n",
       "325         今日头条新版“灵犬”来了，支持图片反低俗  2019-07-30   \n",
       "326  每天有845万人等着他们更新！这些头条上的外国人火了！  2019-07-29   \n",
       "327       被百万人点赞的3个真实故事，网友：从头哭到尾  2019-07-25   \n",
       "328              帮10000个人回家，要多久？  2019-07-25   \n",
       "\n",
       "                                                  link  \\\n",
       "0    http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...   \n",
       "1    http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...   \n",
       "2    http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...   \n",
       "3    http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...   \n",
       "4    http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...   \n",
       "..                                                 ...   \n",
       "324  http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...   \n",
       "325  http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...   \n",
       "326  http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...   \n",
       "327  http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...   \n",
       "328  http://mp.weixin.qq.com/s?__biz=MjM5ODEyOTAyMA...   \n",
       "\n",
       "                                          content_text  \n",
       "0    “多年坚守，在抖音帮人寻亲助百人回家；三进火场，全身烧伤后用歌声回报社会；六年陪伴，用绘画为...  \n",
       "1      荆州博物馆  楚·玉覆面  鄂尔多斯青铜器博物馆  包金卧羊带具  四川广汉三星堆博物馆...  \n",
       "2    一条回家的路可以走多远？2020年疫情期间，寻人志愿者刘红涛在抖音上收到了一条粉丝留言，说是...  \n",
       "3    就在今晨，天问一号登陆火星了！天问一号是我国火星探测计划中的一个重要部分。2020年7月23...  \n",
       "4    “一块荒地上，”王龙羽每天最幸福的时刻，就是看着幼儿园里的孩子们入睡。山村里的星星很亮，结束...  \n",
       "..                                                 ...  \n",
       "324  2018年3月至今，今日头条推出的灵犬反低俗助手已经经过了三次迭代。昨天，“灵犬3.0”正式...  \n",
       "325  ▲一分钟了解灵犬反低俗助手大家好，在这个炎热的夏天，灵犬反低俗助手迎来了重要升级：新版“灵犬...  \n",
       "326  外国人会唱的第一首中文歌是哪个？剥蛋壳对美国人来说有多难？拿着中国驾照可以在德国开车吗？今日...  \n",
       "327  第1个是关于赴台老兵的故事。视频中的主人公名叫刘德文，是台湾高雄左营区祥和里的里长，也是头条...  \n",
       "328  从团结湖地铁站东南口出来，步行到家的时间是15分钟。三年多来，头条寻人项目负责人曾华已经习惯...  \n",
       "\n",
       "[324 rows x 4 columns]"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 余下为不重复的部分\n",
    "df_url_out[~df_url_out.duplicated()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将抓取到的内容保存到本地 —— 数据输出\n",
    "with pd.ExcelWriter('{公众号}公众号链接及文章内容.xlsx'.format(公众号=公众号),mode='w',engine=\"openpyxl\") as writer:  \n",
    "            df_url_out.to_excel(writer, sheet_name=公众号)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
